//
//  AppDelegate.m
//  Fragrance
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "AppDelegate.h"
#import "DataBaseManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    if ([VAGuideView shouldShowGuide]) {
        NSMutableArray *arrImage = [NSMutableArray array];
#warning  这里填你们到导航图  for循环控制张数   后面是图片的名字
        for (int i=0; i<4; i++) {
            //                        UIImage *image = [UIImage imageWithFileName:[NSString stringWithFormat:@"1", @"2", i] ofType:@"png"];
            NSString *name = [NSString stringWithFormat:@"yindaotu%d", i];
            UIImage *image = [UIImage imageNamed:name];
            
            [arrImage addObject:image];
        }
        _guideView = [VAGuideView guideViewWithFrame:self.window.bounds arrImages:arrImage space:30];
        //引导页背景图
        UIImageView *imageViewBg = [[UIImageView alloc] initWithFrame:_guideView.bounds];
#warning 这里是播放导航图时的背景图片  换成你们自己的即可
        imageViewBg.image = [UIImage imageNamed:@"iPhone_guide_bg"];
        imageViewBg.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        imageViewBg.contentMode = UIViewContentModeScaleAspectFill;
        imageViewBg.clipsToBounds = YES;
        [_guideView insertSubview:imageViewBg atIndex:0];
        _guideView.imageContentMode = UIViewContentModeCenter;
        
        //最后一页进入应用按钮
        UIButton *btnStart = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnStart setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
#warning  这是最后一页导航图上面按钮的控制，图片，文字，位置 点击方法（点击之后才创建首页视图,因为导航图消失时有动画效果，建议加个定时器，1秒之后才开始创建视图）
        [btnStart setBackgroundImage:[UIImage imageNamed:@"iPhonse-anniubox-2"] forState:UIControlStateNormal];
        [btnStart setTitle:@"进入应用" forState:UIControlStateNormal];
        btnStart.titleLabel.textAlignment = 1;
        [btnStart setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        btnStart.frame = CGRectMake(0, self.window.frame.size.height - 100, self.window.frame.size.width, 40);
        [btnStart addTarget:self action:@selector(touchStartButton) forControlEvents:UIControlEventTouchUpInside];
        btnStart.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin;
        [_guideView addButtonAtLastPage:btnStart];
        if (_guideView && arrImage.count>0) {
            _currInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
            _lockRotate = YES;
            [self.window addSubview:_guideView];
            double delayInSeconds = 0;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [_guideView startAnimationWithDuration:3
                                 animationComplecation:nil
                                            didDismiss:^{
                                                _lockRotate = NO;
                                            }];
            });
        }
    }
#warning 不是第一次启动，直接创建本页面视图
    else
    {
        _currInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
        _lockRotate = YES;
        [self createMainPageView];
    }
    
    
    
    return YES;
}

- (void)touchStartButton
{
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(createMainPageView) userInfo:nil repeats:NO];
}

- (void)createMainPageView
{
    NSLog(@"%@", NSHomeDirectory());
    [DataBaseManager openDataBase];
    [DataBaseManager createTable];
    HomePageViewController *homePage = [[[HomePageViewController alloc] init] autorelease];
    UINavigationController *hnav = [[[UINavigationController alloc] initWithRootViewController:homePage] autorelease];
    
    
    hnav.navigationBarHidden = NO;
    hnav.navigationBar.translucent = NO;
    hnav.navigationBar.barTintColor = [UIColor colorWithRed:242 / 255.0 green:254 / 255.0 blue:250 / 255.0 alpha:1];
    UIImage *image1 = [UIImage imageNamed:@"2"];
    UIImage *image2 = [UIImage imageNamed:@"1"];
    
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    hnav.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"home" image:image1 selectedImage:image2];
    
    
    
    ClassificationViewController *classifition = [[[ClassificationViewController alloc] init] autorelease];
    UINavigationController *Cnav = [[[UINavigationController alloc] initWithRootViewController:classifition] autorelease];
    Cnav.navigationBarHidden = NO;
    Cnav.navigationBar.translucent = NO;
    UIImage *image3 = [UIImage imageNamed:@"3"];
    UIImage *image4 = [UIImage imageNamed:@"4"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Cnav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"classify" image:image3 selectedImage:image4];
    Cnav.navigationBar.barTintColor = [UIColor colorWithRed:244 / 255.0 green:254 / 255.0 blue:250 / 255.0 alpha:1];
    
    
    OvenViewController *ovc = [[[OvenViewController alloc] init] autorelease];
    UINavigationController *Onav = [[[UINavigationController alloc] initWithRootViewController:ovc] autorelease];
    Onav.navigationBarHidden = NO;
    Onav.navigationBar.translucent = NO;
    UIImage *image5 = [UIImage imageNamed:@"7"];
    UIImage *image6 = [UIImage imageNamed:@"8"];
    image5 = [image5 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image6 = [image6 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Onav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"dinner" image:image5 selectedImage:image6];
    Onav.navigationBar.barTintColor = [UIColor colorWithRed:205 / 255.0 green:166 / 255.0 blue:207 / 255.0 alpha:0.1];
    
    
    
    PersonalViewController *personal = [[[PersonalViewController alloc] init] autorelease];
    UINavigationController *pnav = [[[UINavigationController alloc] initWithRootViewController:personal] autorelease];
    pnav.navigationBarHidden = NO;
    pnav.navigationBar.translucent = NO;
    UIImage *image7 = [UIImage imageNamed:@"5"];
    UIImage *image8 = [UIImage imageNamed:@"6"];
    image7 = [image7 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    image8 = [image8 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    pnav.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"personal" image:image7 selectedImage:image8];
    
    
    
    
    UITabBarController *tabbarcontroller = [[[UITabBarController alloc] init] autorelease];
    tabbarcontroller.tabBar.barTintColor = [UIColor whiteColor];
    tabbarcontroller.tabBar.tintColor = [UIColor blueColor];
    tabbarcontroller.viewControllers = [NSArray arrayWithObjects:hnav, Cnav, Onav, pnav, nil];
    tabbarcontroller.hidesBottomBarWhenPushed = YES;
    self.window.rootViewController = tabbarcontroller;
    
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:242 / 255.0 green:253 / 255.0 blue:250 / 255.0 alpha:1]];
    [[UINavigationBar appearance]setTranslucent:NO];
    
    
    
    [_window release];
    
    
    
    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"5567c23767e58eacd50014c3" url:@"http://api.2meiwei.com/v1/index/"];
    
    [UMSocialData setAppKey:@"5567c23767e58eacd50014c3"];
    
}





- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    return  [UMSocialSnsService handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return  [UMSocialSnsService handleOpenURL:url];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
